<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>出租车的最大盈利</title>
</head>

<body>
  <script>
    var maxTaxiEarnings = function (n, rides) {
      const dp = new Array(n + 1).fill(0);
      dp[0] = 0;
      const hash = {};

      for (let ride of rides) {
        const [st, end, tip] = ride;
        if (hash[end] === undefined) hash[end] = [];
        hash[end].push([st, end - st + tip]);
      }

      for (let i = 1; i <= n; i++) {
        dp[i] = dp[i - 1];
        if (hash[i]) {
          for (let ride of hash[i]) {
            const [st, p] = ride;
            dp[i] = Math.max(dp[i], dp[st] + p);
          }
        }
      }

      return dp[n];
    };
    console.log(maxTaxiEarnings(5, [[2, 5, 4], [1, 5, 1]]));
  </script>
</body>

</html>